import instance from "../url/user.js";
import {user_Store} from "../store/user.js"
import { failure, success, warning } from "@/static/js/alert.js";
const create_ac =async function (account,password) { 
    return await instance.post(`/blog/create_ac?ac=${account}&ps=${password}`)
        .then((response) => { 
            if (response == 0) { failure("注册失败!") }
            if (response == 400) { failure("账户已经被注册!") }
            if (response == 1) {
                return response
            }
        })
} 
const resetac = async function (formdata) {
   
    return await instance.post(`/blog/reset_ac`, formdata)
        .then((response) => {
            if (response == 1) {
                success()
                return response
            }
            if (response == 404) { failure("重置失败!") }
            if (response == 401) { failure("账户不存在!")}
})
}

const check_token = async function () {
    return await instance.get(`/blog/judge/access_token`)
        .then((response) => {
            if (response == 0) { return response}
            else if (response == 401 || response == 404) {
                warning("登录状态错误!")
                return response
            } else { 
                // 此时是用户包含状态和信息的对象
                return response
            }
     })
};
const login = async function (ac,ps) { 
    return await instance.post(`/blog/token?ac=${ac}&ps=${ps}`,
        {
            // withCredentials: true,  // 允许发送和接收 Cookie
            headers: {
                accept: 'application/json'
            }
        }
    ).then((response) => {
        
            if (response == 0) { return response}
            else if (response == 401) {
                warning("密码错误!")
                return response
            } else if (response ==404) { 
                warning("账号未注册!")
                return response
            } else { 
                return response
            }
        });
}
const logout = async function () { 
    return await instance.post(`/logout`)
}

const get_VerificationCode = async function (sender, account) {
    // 由于后端异步需要时间长一点
    instance.defaults.timeout = 5000;
    success("验证码已发送!")
    return await instance.get(`/blog/VerificationCode/get?sender=${sender}&account=${account}`).then((response) => {
        if (response == 404) {
            failure("账户不存在!")
            return 
        }
        if (response == 500) {
            failure("验证码发送失败!")
            return 
        }
        return response
    })
}
const judge_VerificationCode = async function (id, code) {
    instance.defaults.timeout = 5000;
    return await instance.post  (`/blog/VerificationCode/judge?id=${id}&code=${code}`).then((response) => {
       if (response == 404) {
            failure("验证码错误!")
            return
        }
        if (response == 1) {
            return 1
        }
       
    })
}

const get_user_by_id = async  (id)=> {
    return await instance.get(`/blog/user_info_public?id=${id}`).then((response) => {
        if (response == 404) {
            failure("用户不存在!")
            return
        }
        return response
    })
}
const change_user_status = async (toValue, userid)=>{ 
    return await instance.post(`/blog/status/change?status=${toValue}&userid=${userid}`)

}

export { 
    create_ac,
    check_token,
    login,
    logout,
    resetac,
    get_VerificationCode,
    judge_VerificationCode,
    get_user_by_id,
    change_user_status
}